﻿namespace Trading

open System
open System.Reactive.Linq
open System.Reactive.Threading.Tasks

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Observable = 
    let inline iterThrough onNext (source : IObservable<_>) = source.Do(Action<_>(onNext))
    let inline empty<'T> = Observable.Empty<'T>()
    let inline takeWhile (predicate : 'a -> bool) source = Observable.TakeWhile(source, Func<'a, bool>(predicate))
    
    type Builder() =
        member self.Bind(computation : IObservable<'T>, binder : 'T -> IObservable<'TResult>) = computation.SelectMany(binder)
        member self.Yield(value) = Observable.Return(value)
        member self.YeildFrom(value) = value
        member self.Zero() = Observable.Empty()

    let observable = Builder()

    let (>>) (source : IObservable<'a>) (other : IObservable<'b>) = source.SelectMany(other)

[<AutoOpen>]
module AsyncExtensions =
    type Async with
        static member AwaitObservable(source : IObservable<_>) = 
            source.ToArray().ToTask() |> Async.AwaitTask 
//            async {
//                let result = source.ToArray()
//                use task = result.ToTask()
//                return Async.AwaitTask task 
//            }
